extends Camera2D
class_name BattleFieldCamera

@export var left = 0
@export var up = 0
@export var right = 16
@export var down = 16

var width
var height


func _ready() -> void:
	width = get_viewport_rect().size.x
	height = get_viewport_rect().size.y


func setBorder(_left, _up, _right, _down):
	left = _left
	up = _up
	right = _right
	down = _down
	

func moveLeft(dist):
	var targetPos = offset
	targetPos.x -= dist
	setPos(targetPos)
	

func moveUp(dist):
	var targetPos = offset
	targetPos.y -= dist
	setPos(targetPos)
	

func moveRight(dist):
	var targetPos = offset
	targetPos.x += dist
	setPos(targetPos)
	

func moveDown(dist):
	var targetPos = offset
	targetPos.y += dist
	setPos(targetPos)


func setPos(pos):
	pos = trimPosByBorder(pos)
	offset = pos
	

func setFocus(pos:Vector2):
	setPos(pos - Vector2(width/2.0, height/2.0))
	
	
func trimPosByBorder(pos:Vector2):
	if pos.x < left:
		pos.x = left
	if pos.y < up:
		pos.y = up
	if pos.x + width >= right:
		pos.x = right - width
	if pos.y + height >= down:
		pos.y = down - height
	return pos
	

func isPosBeyondView(pos):
	return pos.x < offset.x or pos.y < offset.y or pos.x >= offset.x + width or pos.y >= offset.y + height
	
	
